package com.cyl.manager.pms.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.Excel;

import io.swagger.annotations.ApiModelProperty;

@Service
public class RedisStockService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String STOCK_PRODUCT_KEY = "stock:product:";

    // Lua脚本：原子扣减库存
    private static final String STOCK_DEDUCTION_SCRIPT =
            "local key = KEYS[1]\n" +
            "local quantity = tonumber(ARGV[1])\n" +
            "local current = tonumber(redis.call('GET', key) or '0')\n" +
            "if current >= quantity then\n" +
            "    redis.call('DECRBY', key, quantity)\n" +
            "    return 1\n" +
            "else\n" +
            "    return 0\n" +
            "end";

    
    
    public  class SkuStock {
    	
    	 public SkuStock() {
 			super();
 		}
    	 
        public SkuStock(String skuId, String stock) {
			super();
			this.skuId = Long.parseLong(skuId);
			this.stock = Integer.parseInt(stock);
		}
        
		private Long skuId;
        private Integer stock;
		public Long getSkuId() {
			return skuId;
		}
		public void setSkuId(Long skuId) {
			this.skuId = skuId;
		}
		public Integer getStock() {
			return stock;
		}
		public void setStock(Integer stock) {
			this.stock = stock;
		}
		 
        
        
   }
    
    /**
     * 	设置库存
     */
    public void setStock(Long skuId, int quantity) {
        String redisKey = STOCK_PRODUCT_KEY + skuId;
    	redisTemplate.opsForValue().set(redisKey, String.valueOf(quantity));
    }
    
    /**
     * 扣减Redis库存
     */
    public boolean deductStock(Long skuId, int quantity) {
        String redisKey = STOCK_PRODUCT_KEY + skuId;
        RedisScript<Long> script = new DefaultRedisScript<>(STOCK_DEDUCTION_SCRIPT, Long.class);
        Long result = redisTemplate.execute(script, Collections.singletonList(redisKey), String.valueOf(quantity));
        return result != null && result == 1;
    }

    /**
     * 回滚Redis库存
     */
    public void rollbackStock(Long skuId, int quantity) {
        String redisKey = STOCK_PRODUCT_KEY + skuId;
        redisTemplate.opsForValue().increment(redisKey, quantity);
    }
    

    /**
     * 
     * @return
     */
    public List<RedisStockService.SkuStock> fetchAllStock() {
    	List<RedisStockService.SkuStock> skuStocks = new ArrayList<RedisStockService.SkuStock>();
        Set<String> keys = redisTemplate.keys(STOCK_PRODUCT_KEY + "*");
        for (String key : keys) {
            String value = redisTemplate.opsForValue().get(key);
        	skuStocks.add(new SkuStock(key, value));
        }
		return skuStocks;
    }
}